package me.shurufa.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.bean.DBSearch;
import me.shurufa.event.EventObject;
import me.shurufa.holder.BaseViewHolder;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 搜书列表适配器
 * 新的调豆瓣
 */
public class SearchBookAdapter extends RecyclerView.Adapter<SearchBookAdapter.SearchBookViewHolder> {

    private List<DBSearch.BooksEntity> dataList;
    private Context mContext;
    private int mFromWhere;

    public SearchBookAdapter(Context context, List<DBSearch.BooksEntity> dataList, int fromWhere) {
        this.mContext = context;
        this.dataList = dataList;
        this.mFromWhere = fromWhere;
    }

    @Override
    public SearchBookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_new_search_book, parent, false);
        return new SearchBookViewHolder(v, parent);
    }

    @Override
    public void onBindViewHolder(SearchBookViewHolder holder, int position) {
        DBSearch.BooksEntity book = dataList.get(position);
        holder.bind(book);
    }

    /**
     * 添加数据在尾部
     * @param items datalist
     */
    public void append(List<DBSearch.BooksEntity> items) {
        if (Utils.isNullForList(items)) {
            return;
        }
        int pos = dataList.size();
        dataList.addAll(items);
        notifyItemRangeInserted(pos, items.size());
    }

    /**
     * 添加本（isbn那种只有一个的）
     * @param item
     */
    public void append(DBSearch.BooksEntity item) {
        int pos = dataList.size();
        if (!dataList.contains(item))
            dataList.add(item);
        notifyItemRangeInserted(pos, 1);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void remove(int position) {
        if (dataList.size() > 0) {
            dataList.remove(position);
            this.notifyItemRemoved(position);
        }
    }

    public void clear() {
        int size = dataList.size();
        dataList.clear();
        this.notifyItemRangeRemoved(0, size);
    }

    public class SearchBookViewHolder extends BaseViewHolder {

        @Bind(R.id.book_img)
        ImageView bookImg;
        @Bind(R.id.book_title)
        TextView bookTitle;
        @Bind(R.id.book_author)
        TextView bookAuthor;
        @Bind(R.id.book_factory)
        TextView bookFactory;
        @Bind(R.id.item_root)
        LinearLayout itemRoot;

        @Bind(R.id.tv_in_myshelf)
        TextView tv_in_myshelf;

        private Context mContext;
        private DBSearch.BooksEntity mBook;

        public SearchBookViewHolder(View itemView, Object parent) {
            super(itemView, parent);
            mContext = itemView.getContext();
            ButterKnife.bind(this, itemView);
            addOnClickListener(bookImg);
            addOnClickListener(itemRoot);
        }

        public void bind(DBSearch.BooksEntity book){
            if (book==null){
                return;
            }
            mBook = book;
            if (!TextUtils.isEmpty(book.image)){
                Glide.with(mContext).load(book.image).placeholder(R.drawable.ic_book_loading).error(R.drawable.ic_book_loading).into(bookImg);
            }else {
                bookImg.setImageResource(R.drawable.ic_book_loading);
            }

            if (!TextUtils.isEmpty(book.title)){
                bookTitle.setText(book.title);
            }else {
                bookTitle.setText("未知");
            }

            if (!Utils.isNullForList(book.author)){
                bookAuthor.setText("作者: "+book.author.get(0));
            }else {
                bookAuthor.setText("作者: 未知" );
            }

            if (!TextUtils.isEmpty(book.publisher)){
                bookFactory.setText("出版社: "+ book.publisher);
            }else {
                bookFactory.setText("出版社: 未知");
            }

            tv_in_myshelf.setVisibility(View.GONE);
        }

        @Override
        public void onClick(View v) {
            super.onClick(v);
            switch (v.getId()){
                case R.id.item_root:
                case R.id.book_img:
                    Intent in;
                    switch (mFromWhere) {
                        case Constants.FROM_MAIN_SEARCH:
                            in = new Intent(Constants.EVENT_CLICK_SEARCHBOOK_ITEM_DOUBAN);
                            in.putExtra(Constants.EXTRA_SEARCH_ISBN, mBook.isbn10);
                            break;
                        case Constants.FROM_SEARCH:
                            in = new Intent(Constants.EVENT_CLICK_SEARCHBOOK_ITEM_DOUBAN_ADD_TO_SHELF);
                            in.putExtra(Constants.EXTRA_SEARCH_ISBN, mBook.isbn10);
                            break;
                        default:
                            in = new Intent(Constants.EVENT_CLICK_SEARCHBOOK_ITEM_DOUBAN);
                            in.putExtra(Constants.EXTRA_SEARCH_ISBN, mBook.isbn10);
                            break;
                    }
                    EventBus.getDefault().post(in);
                    break;
            }
        }
    }
}
